<?php

/*
 * This file will return the message from a logId.
 * 
 * Created by: Peter Agno Jr.
 * Date created: October 22, 2011
 * 
 * In parameters: logId
 * Out parameters: message
 */

// Start - Checker for those users who will just go to the page by typing directly in the url.
if ($_POST) {   
    // INCLUDES CONFIGURATION VARIABLES FOR DATABASE ACCESS
    include('../includes/siteConfig.php');
    
    // CONNECT TO DATABASE
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
    
    // GET VARIABLES VIA POST METHOD
    $logId = $_POST['logId'];
    
    // QUERY THE MESSAGE
    $query = 
        "
            SELECT message
            FROM LogMessage
            WHERE logId = '$logId'
        ";
    
    $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    $row = mysql_fetch_array($result);
    
    // Start -  Arrange the message properly
    $arrangedMsg = "";
    $logMsg = explode(".", $row['message']);
    $flagFirst = true;
    $totalMsg = count($logMsg);
    $i = 0;
    
    foreach ( $logMsg as $msg ) {
        
        // Check if new line
        if ( $flagFirst == true ) {
            $flagFirst = false;
        }
        else {
            $arrangedMsg = $arrangedMsg . "<br/>";
        }
        
        $arrangedMsg = $arrangedMsg . $msg;
        
        // Check if there will be a dot
        if ( $i < (count($logMsg) - 1) ) {
            $arrangedMsg = $arrangedMsg . ".";
        }
        
        $i++;
    }
    // End -  Arrange the message properly

    $feed['message'] = $arrangedMsg;
    
    echo json_encode($feed);
    
    mysql_free_result($result);
    mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
 echo "You are not authorized to view this page. This incident will be reported immediately.";
}
?>
